<?php

namespace Apps\AppApi\Method\MyBranch;

use Apps\AppApi\Method\BaseMethod;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\BeautyParlorService;
use Apps\Common\Libs\PicUrl;


/**
 * 获取分店列表
 * @author      545
 * @date        2016年3月4日
 * @version     1.1.2
 * @copyright   Copyright 2015 meelier.com
 */
class ServiceList extends BaseMethod
{
    
    public function run()
    {
        //验证是否登录
        if(false == $this->verifyUserAuth()){
            return false;
        }
        
        $id = intval($this->getDataItem('bp_id',0));
        $page = intval($this->getDataItem('page',1));
        $type = intval($this->getDataItem('type',1));//查询类型 1 可预约， 0 已下架
        $limit = $this->getConfig()->limit;
        $offset = ($page-1)*$limit;

        $type = $type==1?1:-1;
        if(0 < $id) {
            $service = BeautyParlorService::query()
                ->columns([
                'service_id id',
                'service_cover cover',
                'service_name name',
                'service_price price',
                'service_unit unit',])
                ->where('service_state >0 and service_is_sell = :type: and beauty_parlor_id=:bid:')
                ->bind(['bid'=>$id,'type'=>$type])
                ->limit($limit,$offset)
                ->orderBy('service_addtime DESC')
                ->execute()->toArray();
        } else {
            return $this->errorLog(ResultStatus::URL_PARAM_CANNOT_EMPTY, '参数异常');
        }
        
        $services = [];
        if ($service) {
            foreach($service as $d) {
                $d['cover'] = PicUrl::BeautyParlorCover($d['cover'], $this->getDi());
                $unit = $d['unit'];
                unset($d['unit']);
                $d['price_show'] = '';
                if($d['price'] > 0) {
                    $d['price_show'] = $d['price'] . '元' . ($unit ? '/' . $unit : '');
                }
                $services[] = $d;
            }
            $this->setResult($services);   
        }else{
            $this->setResult($services);
        }
    }
    

}